use std::path::PathBuf;

use serde::{Deserialize, Deserializer, Serialize};

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ProjectConfig {
    pub name: String,
    pub scene: String,
    pub path: Option<PathBuf>,
    pub current_scene: Option<String>,
}

impl ProjectConfig {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            scene: String::new(),
            path: None,
            current_scene: None,
        }
    }

    pub fn with_scene(mut self, scene: &str) -> Self {
        self.scene = scene.to_string();
        self
    }

    pub fn get_path_str(&self) -> &str {
        self.path.as_ref().unwrap().to_str().unwrap()
    }

    pub fn get_path(&self) -> &PathBuf {
        self.path.as_ref().unwrap()
    }

    pub fn to_file(&self) -> String {
        let file = ProjectConfigFile {
            name: self.name.clone(),
            scene: self.scene.clone(),
        };

        ron::to_string(&file).unwrap()
    }
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ProjectConfigFile {
    pub name: String,
    pub scene: String,
}

impl Into<ProjectConfig> for ProjectConfigFile {
    fn into(self) -> ProjectConfig {
        ProjectConfig::new(&self.name).with_scene(&self.scene)
    }
}
